/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jard.webshop.nbp;

import jard.webshop.util.Constants;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

/**
 *
 * @author CJP
 */
@Entity
@NamedQueries(
@NamedQuery(name = Constants.GET_ALL_RCPTS, query = "SELECT r FROM Receipt r"))
public class Receipt implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private Long orderNumber = 0L;
    @Column(nullable=false)
    private String name = "";
    @Column(nullable=false)
    private String streetAddress = "";
    @Column(nullable=false)
    private Integer zipCode = 0;
    @Column(nullable=false)
    private String area = "";
    @Column(nullable=false)
    private Float total = 0f;
    @OneToMany(cascade=CascadeType.ALL)
    private List<ReceiptEntry> entries;

    public Receipt() {
        entries = new ArrayList<ReceiptEntry>();
    }
    
    public Receipt(Invoice invoice) {
        User user = invoice.getCustomer();
        name = user.getName();
        streetAddress = user.getAddress().getStreetWithNumber();
        zipCode = user.getAddress().getZipCode();
        area = user.getAddress().getArea();
        total = invoice.getTotalAmount();

        entries = new ArrayList<ReceiptEntry>();
        for(InvoiceEntry ie : invoice.getEntries()) {
            ReceiptEntry re = new ReceiptEntry(ie);
            entries.add(re);
        }
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
    
    public Long getOrderNumber() {
        return orderNumber;
    }

    public void setOrderNumber(Long orderNumber) {
        this.orderNumber = orderNumber;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Receipt)) {
            return false;
        }
        Receipt other = (Receipt) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        String retVal = "";
        for(ReceiptEntry re : entries) {
            retVal += "\n|" + re.toString();
        }
        return "jard.webshop.nbp.Receipt[ id=" + id + ", " + name + ", " + orderNumber + " ]" + retVal;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStreetAddress() {
        return streetAddress;
    }

    public void setStreetAddress(String streetAddress) {
        this.streetAddress = streetAddress;
    }

    public Integer getZipCode() {
        return zipCode;
    }

    public void setZipCode(Integer zipCode) {
        this.zipCode = zipCode;
    }

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }

    public Float getTotal() {
        return total;
    }

    public void setTotal(Float total) {
        this.total = total;
    }

    public List<ReceiptEntry> getEntries() {
        return entries;
    }

    public void setEntries(List<ReceiptEntry> entries) {
        this.entries = entries;
    }

    
    
}
